import datetime
from flask import Blueprint,request,jsonify
from db import db1
from comm import get_data
bp_news = Blueprint('news',__name__)

@bp_news.route('/domesticM',methods=['GET','POST'])
def domesticM():
    if request.method == 'GET':
        sql = "select id,name from domestic"
        rse = db1.find_all(sql)
        return jsonify({'code':200,'list':rse})
    else:
        data = get_data()
        sql = "insert into domestic values(0,'%s')"%(data['name'])
        db1.insert(sql)
        return jsonify({'code':200})

@bp_news.route('/gameM',methods=['GET','POST'])
def gameM():
    if request.method == 'GET':
        id = request.args.get('id')
        if not id:
            sql = "select id,title,dom_id from game where dom_id=(select id from domestic)"
            rse = db1.find_all(sql)[:2]
            return jsonify({'code':200,'list':rse})
        else:
            page = int(request.args.get('page'))
            page_size = int(request.args.get('page_size'))
            start = (int(page)-1)*page_size
            sql = "select id,title,dom_id from game limit %d,%d"%(start,page_size)
            rse = db1.find_all(sql)
            sql1 = "select * from game where dom_id=(select id from domestic)"
            rse1 = db1.find_all(sql1)
            return jsonify({'code':200,'list':rse1})
    else:
        data = get_data()
        sql = "insert into game values(0,'%s',%d)"%(data['title'],data['dom_id'])
        db1.insert(sql)
        return jsonify({'code':200})


@bp_news.route('/sendmoney')
def sendmoney():
    morey = request.args.get('morey')
    if not morey:
        return jsonify({'code':20010,'list':'输入金额不能为空'})
    else:
        try:
            morey = float(morey)
        except:
            return jsonify({'code':20011,'list':'格式不正确'})
    if 0.1<morey<200:
        return jsonify({'code':200})
    else:
        return jsonify({'code':20012,'list':'输入金额超出指定值'})

@bp_news.route('/sendMoney',methods=['POST'])
def sendMoney():
    data = get_data()
    sql = "insert into test_record(name,url,method,params,result) values ('%s','%s','%s','%s','%s')"%(data['name'],data['url'],data['method'],data['params'],data['result'])
    db1.insert(sql)
    return jsonify({'code':200})

import requests,json,os
@bp_news.route('/test_sendMoney')
def test_sendMoney():
    sql = "select * from test_record where status=1"
    res = db1.find_all(sql)
    list = []
    for i in res:
        flag =  "失败"
        params = json.loads(i['params'])
        if i['method'] == 'get':
            mes = requests.get(i['url'],params=params)
        elif i['method'] == 'post':
            mes = requests.post(i['url'],params=params)
        mm = json.loads(mes.text)
        if mm['code'] == int(i['result']):
            flag = "成功"
        str = {'name':i['name'],'请求地址':i['url'],'返回信息':mes.text,'测试结果':flag}
        list.append(str)
        filename = datetime.datetime.strftime(datetime.datetime.now(),'%Y,%m%d')
        with open(os.path.join('./logs/'+filename+'.txt'),'w') as f:
            f.write(json.dumps(list,ensure_ascii=False))
    return jsonify({'code':200})